<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BgcmsPage extends BgcmsProcess {
	var $db;
 	var $smarty;	
	public function __construct($db, $smarty) {
		$this->db = $db;
		$this->smarty = $smarty; 	
	}
		
	public function __destruct() {
		//
	}
	
	public function process(){
		$this->Run();
	}
	
	public function create_page() {
		if(isset($_POST['page_type']) && !empty($_POST['page_type'])) {
    		$existed = BgcmsPageBean::checkPagePositionExist($this->db, $_POST['page_type']);
		}
		if($existed == null) {
			$res = BgcmsPageBean::save_page($this->db, $_POST);
			BgcmsPageBean::save_assign_page($this->db, $res,$_POST['cat_id']);
		} else {
			$res = -99;
		}
		if($res > 0) {
				$_SESSION['page_message'] = "Page has been saved.";
			} elseif($res == -99) {
				$_SESSION['page_message'] = "Page with selected header position existed.";
			} else {
				$_SESSION['page_message'] = "Page doesn't saved.";
			}
		if(isset($_POST['type']) && $_POST['type']!="ajax") {
			
			if($_POST['getlist'] != "")
				$getlist = "&list=".$_POST['getlist'];
			if($_POST['parent_id'] != ""){
				$parent_id = BgcmsCategoryBean::get_parent_id_name_by_id($this->db,$_POST['cat_id']);
				header("Location: ./?module=BgcmsContentManager&subcat=load_sub_category&catid=".$parent_id.$getlist);	
			}else {
					header("Location: ./?module=BgcmsContentManager".$getlist);	
			}
			$message = "Page has been added";
			setcookie("message", $message,time()+5);
		} else {
			if($_POST['parent_id'] != ""){
				$parent_id = BgcmsCategoryBean::get_parent_id_name_by_id($this->db,$_POST['cat_id']);
				header("Location: ./?module=BgcmsContentManager&subcat=load_sub_category&catid=".$parent_id."&act=assign_page&id=".$_POST['cat_id']);	
			}else {
					header("Location: ./?module=BgcmsContentManager&act=assign_page&id=".$_POST['cat_id']);	
			}
			$message = "Page has been added";
			setcookie("message", $message,time()+5);
		}
	}
	
	public function edit_page() {
		if(isset($_POST['page_type']) && !empty($_POST['page_type'])) {
    		$existed = BgcmsPageBean::checkPagePositionExist($this->db, $_POST['page_type']);
    	}
    	
    	if(($existed == NULL) || ($existed->page_id == $_POST['id'])) {
			$res = BgcmsPageBean::update_page($this->db, $_POST);
    	} else {
    		$res = -99;
    	}
		
		if(isset($_POST['type']) && $_POST['type']!="ajax") {
			if($res == 0) {
				$_SESSION['page_message'] = "Page has been updated.";
			} elseif($res == -99) {
				$_SESSION['page_message'] = "Page with selected header position existed.";
			} else {
				$_SESSION['page_message'] = "Unable to update Page.";
			}
			if($_POST['getlist'] != "")
				$getlist = "&list=".$_POST['getlist'];
			if($_POST['parent_id'] != ""){
				header("Location: ./?module=BgcmsContentManager&sub1=category&subcat=load_sub_category&catid=".$_POST['parent_id'].$getlist);	
			}else {
					header("Location: ./?module=BgcmsContentManager&sub1=category".$getlist);	
			}
			$message = "Page has been updated";
			setcookie("message", $message,time()+5);				
		} else {
			if($_POST['parent_id'] != ""){
				$parent_id = BgcmsCategoryBean::get_parent_id_name_by_id($this->db,$_POST['cat_id']);
				header("Location: ./?module=BgcmsContentManager&subcat=load_sub_category&catid=".$parent_id."&act=assign_page&id=".$_POST['cat_id']);	
			}else {
					header("Location: ./?module=BgcmsContentManager&act=assign_page&id=".$_POST['cat_id']);	
			}
			$message = "Page has been updated";
			setcookie("message", $message,time()+5);
		}
	}
	
	public function delete_page(){
		$arr = array("id" => -1, "code" => -1, "message" => _("Page doesn't deleted."));	
		$id = "";
		if(isset($_POST['id'])) {
			$id = $_POST['id'];
			$res = BgcmsPageBean::delete_page($this->db, $id);
			if($res > 0){
				$arr['code'] = 0;
				$arr['message'] = _("Page has been deleted!");
			}
		}
		echo array2json($arr);
	}
}
?>